package org.proddevplm.dao.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.proddevplm.dao.api.IRolesMgmt;
import org.proddevplm.dao.data.RelationMatrix;
import org.proddevplm.dao.data.Roles;
import org.proddevplm.dao.data.User;
import org.proddevplm.dao.util.HibernateUtil;

class RolesMgmt implements IRolesMgmt{

	public List<String> getRoles() {
		Session session = HibernateUtil.currentSession();
		List<Roles> rolesResult = session.createQuery("select role from Roles as role").list();
		List<String> roles = new ArrayList<String>();
		for (Roles r:rolesResult){
			roles.add(r.getRole());
		}
		return roles;
	}
	
	public Map<String, Roles> getRolesByName(String[] rolesArray){
		
		Map<String, Roles> rolesMap = new HashMap<String, Roles>();
		Session session = HibernateUtil.currentSession();
		List<String> roleNames = new ArrayList<String>();
		for (String str:rolesArray){
			roleNames.add(str);
		}
		String query="select role from Roles role where role.role in (:roleNames)";
		
		if (!roleNames.contains("Project Manager")){
			roleNames.add("Project Manager");			
		}
		Query q = session.createQuery(query);
		q.setParameterList("roleNames", roleNames);
 	
		List<Roles> results = q.list();
		for(Roles r: results){
			rolesMap.put(r.getRole(), r);
		}
		return rolesMap;
	}

	@Override
	public List<Roles> getAllRoles() {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select r from Roles as r");
			
		return q.list();
	}

	@Override
	public Map<Integer, Roles> getRolesById(String[] rolesArray) {

		Map<Integer, Roles> rolesMap = new HashMap<Integer, Roles>();
		Session session = HibernateUtil.currentSession();
		List<Integer> roleIds = new ArrayList<Integer>();
		for(String str:rolesArray){
			roleIds.add(Integer.valueOf(str));
		}
		Query q = session.createQuery("select r from Roles r where r.id in (:roleIds)");
		q.setParameterList("roleIds", roleIds);
		List<Roles> results = q.list();
		for (Roles role:results){
			rolesMap.put(role.getId(), role);
		}
		return rolesMap;
	}

	@Override
	public Roles getRoleById(int roleId) {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select r from Roles r where r.id=:roleId");
		q.setInteger("roleId", roleId);
		return (Roles)q.uniqueResult();
	}

	@Override
	public List<Roles> getRolesByProjectId(int projectId) {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select r from RelationMatrix r where r.project.id=:projectId");
		q.setInteger("projectId", projectId);
		List<RelationMatrix> matrixResults = q.list();
		List<Roles> rolesResults = new ArrayList<Roles>(matrixResults.size());
		
		/*int i=0;
		while (i<matrixResults.size()){
			RelationMatrix r = matrixResults.get(i);
			Roles role = r.getRole();
			if (!rolesResults.contains(role)){
				rolesResults.add(role);
			}
		}*/
		
		for (int i=0; i<matrixResults.size(); i++){
			RelationMatrix r = matrixResults.get(i);
			Roles role = r.getRole();
			if (!rolesResults.contains(role)){
				rolesResults.add(role);
			}
		}

		
		return rolesResults;
	}

	@Override
	public Roles getRoleByName(String role) {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select r from Roles r where r.role=:role");
		q.setString("role", role);
		
		return (Roles)q.uniqueResult();
	}

	@Override
	public boolean addRole(Roles role) {
		
		HibernateUtil.currentSession().persist(role);
		
		return false;
	}
}
